package com.domain.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.domain.model.Sysrole;
import com.domain.model.Sysuser;
import com.domain.service.RoleService;

/**
 * 角色权限管理 Controller
 * @author wenkai
 * @since 2015年4月20日 
 */
@Controller
@RequestMapping("/role")
public class RoleController extends SuperController{
	@Resource
	private RoleService roleService;
	
	/**
	 * 进入角色添加页面
	 */
	@RequestMapping("/toAddRole/{porCode}.html")
	public String toAddRole(@PathVariable String porCode,ModelMap model){
		model.put(POR_CODE, porCode);
		return "system/addRole";
	}
	
	/**
	 * 角色添加
	 * @param role  角色对象 
	 */
	@RequestMapping("/addRole/{porCode}.html")
	public String doAddRole(Sysrole role,@PathVariable String porCode,ModelMap model){
		roleService.doAdd(role);
		 return "redirect:/role/toList.html";
	}
	
	/**
	 * 查询角色列表
	 */
	@RequestMapping("/toList.html")
	public String toRoleList(HttpServletRequest request,String porCode,ModelMap model){
		//userService.doAdd(user);
		List<Sysrole> roles = roleService.doRoleList();
		request.setAttribute("roles", roles);
		model.put(POR_CODE, porCode);
		return "system/roleList";
	}
	
	
	/**
	 * 进入角色编辑页面
	 */
	@RequestMapping("/toEditRole/{porCode}.html")
	public String toEditRole(HttpServletRequest request,Long id,@PathVariable String porCode,ModelMap model){
		Sysrole role = roleService.getRoleById(id);
		request.setAttribute("role", role);
		model.put(POR_CODE, porCode);
		return "system/editRole";
	}

	/**
	 * 编辑角色信息角色
	 */
	@RequestMapping("/editRole/{porCode}.html")
	public String doEditRole(Sysrole role,@PathVariable String porCode,ModelMap model){
		try {
			model.addAttribute(POR_CODE, porCode);
			roleService.editRole(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		   return "redirect:/role/toList.html";
	}
	
	/**
	 * 进入角色编辑页面
	 */
	@RequestMapping("/toRightSet/{porCode}.html")
	public String toRightSet(HttpServletRequest request,Long id,@PathVariable String porCode,ModelMap model){
		List<Map<String, Object>> list = roleService.getRightByRoleid(id);
		request.setAttribute("list", list);
		request.setAttribute("id", id);
		return "system/rightSet";
	}
	
	/**
	 * 进入角色编辑页面
	 */
	@RequestMapping("/rightSet/{porCode}.html")
	public String doRightSet(Long id,String nodeId,@PathVariable String porCode,ModelMap model){
		try {
			roleService.doRightSet(id, nodeId);
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		model.addAttribute(POR_CODE, porCode);
		return "redirect:/role/toList.html";
	}
	
	/**
	 * 进入角色编辑页面
	 */
	@RequestMapping("/left.html")
	public String toLeft(HttpServletRequest request){
		try {
			Sysuser user =super.getCurrentUser(request);
			List<Map<String, Object>> list = roleService.getMenuByRoleid(user.getRoleId());
			request.setAttribute("list", list);
			return "frame/left";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "exception";
		}
	}
	
}
